/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: viewer.cpp 330 2009-03-02 18:08:45Z jpnurmi $
*/

#include "viewer.h"
#include "mdiarea.h"
#include "tabwidget.h"
#include "messageview.h"
#include <QVBoxLayout>
#include <QAction>
#include <QtDebug>

Viewer::Viewer(QWidget* parent)
    : QWidget(parent)
{
    d.actionMdiMode = new QAction(tr("&MDI Mode"), this);
    d.actionTabsMode = new QAction(tr("&Tabs Mode"), this);
    d.actionSplitMode = new QAction(tr("&Split Mode"), this);
    d.actionSplitMode->setEnabled(false);
    connect(d.actionMdiMode, SIGNAL(triggered()), this, SLOT(setMdiMode()));
    connect(d.actionTabsMode, SIGNAL(triggered()), this, SLOT(setTabsMode()));
    connect(d.actionSplitMode, SIGNAL(triggered()), this, SLOT(setSplitMode()));
    addActions(QList<QAction*>() << d.actionMdiMode << d.actionTabsMode << d.actionSplitMode);

    d.layout = new QVBoxLayout(this);
    d.layout->setMargin(0);
    d.container = 0;
    d.mode = TabsMode;
    setViewMode(MdiMode);
}

Viewer::~Viewer()
{
    blockSignals(true);
}

Viewer::ViewMode Viewer::viewMode() const
{
    return d.mode;
}

void Viewer::setViewMode(Viewer::ViewMode mode)
{
    if (d.mode != mode)
    {
        QList<MessageView*> views;
        if (d.container)
        {
            views = d.container->takeAllViews();
            if (d.container->container())
                d.container->container()->deleteLater();
            else
                delete d.container;
        }

        d.mode = mode;
        switch (d.mode)
        {
        case MdiMode:
            d.container = new MdiArea(this);
            break;

        case TabsMode:
            {
                TabWidget* tab = new TabWidget(this);
#if QT_VERSION >= 0x040500
                tab->setTabsClosable(true);
                //TODO : Part the channel, remove the view
                connect(tab, SIGNAL(tabCloseRequested(int)), tab, SLOT(closeTab(int)));
#endif // QT_VERSION
                d.container = tab;
                break;
            }

        default:
            Q_ASSERT_X(false, "Viewer::setViewMode", "TODO");
            break;
        }

        QWidget* widget = d.container->container();
        d.layout->addWidget(widget);
        connect(widget, SIGNAL(currentViewChanged(MessageView*)),
            this, SIGNAL(currentViewChanged(MessageView*)));
        widget->show();

        foreach (MessageView* view, views)
            d.container->addView(view);
    }
}

void Viewer::setMdiMode()
{
    setViewMode(MdiMode);
}

void Viewer::setTabsMode()
{
    setViewMode(TabsMode);
}

void Viewer::setSplitMode()
{
    setViewMode(SplitMode);
}

MessageView* Viewer::addMessageView(const QString& receiver)
{
    MessageView* messageView = new MessageView(receiver, d.container->container());
    d.container->addView(messageView);
    return messageView;
}

void Viewer::removeView(MessageView* view)
{
    d.container->removeView(view);
}

MessageView* Viewer::currentView() const
{
    return d.container->currentView();
}

void Viewer::setCurrentView(MessageView* view)
{
    d.container->setCurrentView(view);
}
